import { defHttp } from '/@/common/util/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
  list = '/online/genDataTable/getPage',
  save='/online/genDataTable/add',
  edit='/online/genDataTable/edit',
  deleteOne = '/online/genDataTable/delete',
  updateBatch = '/online/genDataTable/updateBatch',
  deleteBatch = '/online/genDataTable/deleteBatch',
  importExcel = '/online/genDataTable/importExcel',
  exportXls = '/online/genDataTable/exportXls',

  init = '/online/genDataTable/init',                         //初始化数据库表
  updateGenColumn = '/online/genDataTable/updateGenColumn',   //同步指定表字段
  updateTableId = '/online/genDataTable/updateTableId',   //同步指定表主键为id字段
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

export const init = (params) => defHttp.post({ url: Api.init, params });
export const updateGenColumn = (params) => defHttp.post({ url: Api.updateGenColumn+'/'+ params });
export const updateTableId = (params) => defHttp.post({ url: Api.updateTableId+'/'+ params });

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({url: Api.list, params});

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

/**
 * 批量更新
 * @param params
 */
export const updateBatch = (params) => {
  const url = Api.updateBatch;
  return defHttp.post({ url: url, params });
};
/**
 * 批量删除
 * @param params
 */
export const deleteBatch = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}
